package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcLabResultMapper;
import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.entity.CdcLabResultEntity;
import com.dhcc.cdc.busi.service.ICdcLabItemService;
import com.dhcc.cdc.busi.service.ICdcLabResultService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检验结果表数据服务层接口
 * 
 * @ClassName: LabResultServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:21:40
 */
@Service("labResultService")
public class CdcLabResultServiceImpl extends BaseServiceImpl<CdcLabResultMapper, CdcLabResultEntity> implements ICdcLabResultService {
    @Autowired
    ICdcLabItemService labItemSrv;

	@Override
	public List<CdcLabResultEntity> page(Page<CdcLabResultEntity> page, Map<String, Object> map) {
		return warpLabRet(this.baseMapper.page(page, map));
	}
	@Override
	public List<CdcLabResultEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

    @Override
    public List<Long> selectLabItemsByLabVisit(Long labVisitId) {
        return this.baseMapper.selectLabItemsByLabVisit(labVisitId);
    }

    public List<CdcLabResultEntity> warpLabRet(List<CdcLabResultEntity> list){
        Map<Long, Object> cacheDic = new HashMap<Long, Object>();
        for (CdcLabResultEntity labret : list) {
            if (labret.getLabItemId() != null) {
                if (cacheDic.containsKey(labret.getLabItemId())) {
                    labret.setLabItem((CdcLabItemEntity) cacheDic.get(labret.getLabItemId()));
                } else {
                    CdcLabItemEntity orderStatus = this.labItemSrv.selectById(labret.getLabItemId());
                    labret.setLabItem(orderStatus);
                    cacheDic.put(labret.getLabItemId(), orderStatus);
                }
            } else {
                labret.setLabItem(new CdcLabItemEntity());
            }
        }
        return list;
    }
}